<!DOCTYPE html>
<html>
<head>
<title>管理员列表</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="../../plugins/bootstrap/unused/bootstrap.min.css">
<!--<link rel="stylesheet" href="../../plugins/fontawesome/font-awesome.min.css">-->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="../../plugins/jqgrid/ui.jqgrid-bootstrap.css">
<link rel="stylesheet" href="../../plugins/ztree/css/metroStyle/metroStyle.css">
<link rel="stylesheet" href="../../tgflib/css/main.css">

</head>
<body>
<div id="zgfapp" v-cloak>
	<div v-show="showList">
		<div class="grid-btn">
			<div class="form-group col-sm-2">
				<input type="text" class="form-control" v-model="q.username" @keyup.enter="query" placeholder="用户名">
			</div>
			<a class="btn btn-default" @click="query">查询</a>
			<a v-if="hasPermission('sys:user:save')" class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
			<a v-if="hasPermission('sys:user:update')" class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
			<a v-if="hasPermission('sys:user:delete')" class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
		</div>
	    <table id="jqGrid"></table>
	    <div id="jqGridPager"></div>
    </div>
    
    <div v-show="!showList" class="panel panel-default">
		<div class="panel-heading">{{title}}</div>
		<form class="form-horizontal">
			<div class="form-group">
			   	<div class="col-sm-2 control-label">用户名</div>
			   	<div class="col-sm-10">
			      <input type="text" class="form-control" v-model="user.username" placeholder="登录账号"/>
			    </div>
			</div>
			<div class="form-group">
			   	<div class="col-sm-2 control-label">密码</div>
			   	<div class="col-sm-10">
			      <input type="text" class="form-control" v-model="user.password" placeholder="密码"/>
			    </div>
			</div>
			<div class="form-group">
			   	<div class="col-sm-2 control-label">邮箱</div>
			   	<div class="col-sm-10">
			      <input type="text" class="form-control" v-model="user.email" placeholder="邮箱"/>
			    </div>
			</div>
			<div class="form-group">
			   	<div class="col-sm-2 control-label">手机号</div>
			   	<div class="col-sm-10">
			      <input type="text" class="form-control" v-model="user.mobile" placeholder="手机号"/>
			    </div>
			</div>
			<div class="form-group">
			   	<div class="col-sm-2 control-label">角色</div>
			   	<div class="col-sm-10">
				   	<label v-for="role in roleList" class="checkbox-inline">
					  <input type="checkbox" :value="role.roleId" v-model="user.roleIdList">{{role.roleName}}
					</label>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-2 control-label">状态</div> 
				<label class="radio-inline">
				  <input type="radio" name="status" value="0" v-model="user.status"/> 禁用
				</label>
				<label class="radio-inline">
				  <input type="radio" name="status" value="1" v-model="user.status"/> 正常
				</label>
			</div>
			<div class="form-group">
				<div class="col-sm-2 control-label"></div> 
				<input type="button" class="btn btn-primary" @click="saveOrUpdate" value="确定"/>
				&nbsp;&nbsp;<input type="button" class="btn btn-warning" @click="reload" value="返回"/>
			</div>
		</form>
	</div>
</div>


<script src="../../plugins/jQuery/jQuery-2.1.4.min.js"></script>
<script src="../../plugins/layer/layer.js"></script>
<script src="../../plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="../../plugins/vue.min.js"></script>
<script src="../../plugins/validator.min.js"></script>
<script src="../../plugins/jqgrid/grid.locale-cn.js"></script>
<script src="../../plugins/jqgrid/jquery.jqGrid.min.js"></script>
<script src="../../plugins/ztree/jquery.ztree.all.min.js"></script>
<script src="../../tgflib/js/zgfApi.js"></script>
<script type="text/javascript">
    $(function () {
        $("#jqGrid").jqGrid({
            url: baseURL + '/jdata/user.json',// '/sys/sysUser/list',
            datatype: "json",//从服务器端返回的数据类型，默认xml。可选类型：xml，local，json，jsonnp，script，xmlstring，jsonstring，clientside
			mtype: "get", //ajax提交方式。POST或者GET，默认GET
            colModel: [
                { label: '用户ID', name: 'userId', index: "user_id", width: 45, key: true },
                { label: '用户名', name: 'username', width: 75 },
                { label: '邮箱', name: 'email', width: 90 },
                { label: '手机号', name: 'mobile', width: 100 },
                { label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
                        return value === 0 ?
                            '<span class="label label-danger">禁用</span>' :
                            '<span class="label label-success">正常</span>';
                    }},
                { label: '创建时间', name: 'createTime', index: "create_time", width: 80}
            ],
            viewrecords: true,//定义是否要显示总记录数
            height: 385,
            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true, // 如果为ture则会在表格左边新增一列，显示行顺序号，从1开始递增。此列名为'rn'.
            rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        });
    });
    var vm = new Vue({
        el:'#zgfapp',
        data:{
            q:{
                username: null
            },
            showList: true,
            title:null,
            roleList:{},
            user:{
                status:1,
                roleIdList:[]
            }
        },
        methods: {
            query: function () {
                vm.reload();
            },
            add: function(){
                vm.showList = false;
                vm.title = "新增";
                vm.roleList = {};
                vm.user = {status:1,roleIdList:[]};

                //获取角色信息
                this.getRoleList();
            },
            update: function () {
                var userId = getSelectedRow();
                if(userId == null){
                    return ;
                }

                vm.showList = false;
                vm.title = "修改";

                vm.getUser(userId);
                //获取角色信息
                this.getRoleList();
            },
            del: function () {
                var userIds = getSelectedRows();
                if(userIds == null){
                    return ;
                }

                confirm('确定要删除选中的记录？', function(){
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/user/delete",
                        contentType: "application/json",
                        data: JSON.stringify(userIds),
                        success: function(r){
                            if(r.code == 0){
                                alert('操作成功', function(){
                                    vm.reload();
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                });
            },
            saveOrUpdate: function () {
                if(vm.validator()){
                    return ;
                }

                var url = vm.user.userId == null ? "sys/user/save" : "sys/user/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.user),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            },
            getUser: function(userId){
                $.get(baseURL + "sys/user/info/"+userId, function(r){
                    vm.user = r.user;
                    vm.user.password = null;
                });
            },
            getRoleList: function(){
                $.get(baseURL + "sys/role/select", function(r){
                    vm.roleList = r.list;
                });
            },
            reload: function () {
                vm.showList = true;
                var page = $("#jqGrid").jqGrid('getGridParam','page');
                $("#jqGrid").jqGrid('setGridParam',{
                    postData:{'username': vm.q.username},
                    page:page
                }).trigger("reloadGrid");
            },
            validator: function () {
                if(isBlank(vm.user.username)){
                    alert("用户名不能为空");
                    return true;
                }

                if(vm.user.userId == null && isBlank(vm.user.password)){
                    alert("密码不能为空");
                    return true;
                }

                if(isBlank(vm.user.email)){
                    alert("邮箱不能为空");
                    return true;
                }

                if(!validator.isEmail(vm.user.email)){
                    alert("邮箱格式不正确");
                    return true;
                }
            }
        }
    });


</script>
</body>
</html>